package com.shadow.demo.info.controller;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shadow.demo.common.bo.R;
import com.shadow.demo.common.form.SitePublicForm;
import com.shadow.demo.data.BaseController;
import com.shadow.demo.data.bo.LoginUser;
import com.shadow.demo.data.info.entity.SiteInfoEntity;
import com.shadow.demo.info.service.SiteService;
import com.shadow.demo.shiro.util.UserUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

@Slf4j
@DS("info")
@RestController
@RequestMapping("site")
public class SiteController extends BaseController {

    private final SiteService siteService;

    SiteController(SiteService siteService) {
        this.siteService = siteService;
    }

    /**
     * 获取网址类型
     */
    @GetMapping("public/type")
    public R publicType() {
        LoginUser user = UserUtils.getCurrentLoginUser();
        Set<String> roles = siteService.getTypeByRole(Objects.isNull(user) ? null : user.getRoles());
        return R.success(roles);
    }

    /**
     * 获取网址
     *
     * @param type 类型
     */
    @GetMapping("public/list")
    public R publicList(@RequestParam(required = false) String type) {
        log.info("获取网址, part : {}", type);
        LoginUser user = UserUtils.getCurrentLoginUser();

        SitePublicForm form = new SitePublicForm();
        form.setType(type);
        if (!Objects.isNull(user)) {
            form.setRoles(user.getRoles());
        }

        List<SiteInfoEntity> publicList = siteService.getPublicList(form);
        return R.success(publicList);
    }

    @PostMapping("/list")
    public R getList(@RequestParam Map<String, Object> params) {
        Page<SiteInfoEntity> page = siteService.getList(params);
        return successPage(page);
    }

    @PostMapping("/importData")
    public R importData(@RequestParam("excel") final MultipartFile excel) {
        if (excel == null || excel.isEmpty()) {
            return R.fail();
        }
        String msg = siteService.importData(excel);
        return StringUtils.isEmpty(msg)
                ? R.success(msg)
                : R.fail(msg);
    }

    @PostMapping("save")
    public R save(@RequestBody SiteInfoEntity entity) {
        String msg = saveVerify(entity);
        if (msg != null) {
            return R.fail(msg);
        }
        if (siteService.verifyExist(entity.getTitle())) {
            return R.fail("角色已经存在");
        }
        siteService.save(entity);
        return R.success();
    }

    @PostMapping("update")
    public R update(@RequestBody SiteInfoEntity entity) {
        String msg = saveVerify(entity);
        if (msg != null) {
            return R.fail(msg);
        }
        siteService.updateById(entity);
        return R.success();
    }

    @PostMapping("/del/{ids}")
    public R del(@PathVariable("ids") String ids) {
        int change = siteService.delete(ids);
        return R.success(change);
    }

    /**
     * 保存验证
     *
     * @return 通过 null
     * 不通过 msg
     */
    private String saveVerify(final SiteInfoEntity entity) {
        if (StringUtils.isBlank(entity.getTitle())) {
            return "名称不能为空";
        }
        return null;
    }
}
